/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.iwords.web.teacher;


import com.iwords.service.TeacherService;
import com.iwords.service.object.TeacherEntity;
import com.iwords.service.object.UserEntity;
import java.io.IOException;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author yunzi
 */
@WebServlet(name = "TeacherServlet", urlPatterns = {"/teacher"})
public class TeacherServlet extends BaseServlet<TeacherEntity> {

    /**如果command不能match到任何方法,默认显示的页面*/
    @Override
    protected void defaultAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        forward(request, response, "/user/login.jsp");
    }

    void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("UserServlet.register() idParam = "+request.getParameter("id"));
        String idParam = request.getParameter("idParam");
        TeacherEntity teacher = null;
        if (idParam != null && idParam.length() > 0) {
            teacher = teacherService.find(Integer.valueOf(idParam));
        } else {
            teacher = new TeacherEntity();
        }
        request.setAttribute("user", teacher);
        forward(request, response, "/teacher/teacherInfo.jsp");
    }
    
    
    void save(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String idParam = request.getParameter("teacherid");
        try {
            String password = request.getParameter("password");
            String confirm = request.getParameter("confirm");
            String teachername = request.getParameter("teachername");
            String marketingid = request.getParameter("marketingid");
            if (teachername == null || teachername.length() < 5) {
                throw new Exception("教师名不能为空，且长度至少为5.");
            }
            if (marketingid == null || marketingid.length() < 5) {
                throw new Exception("优惠凭证不能为空，且长度至少为5.");
            }
            if (password == null || password.length() < 1 || confirm == null || confirm.length() < 1) {
                throw new Exception("密码不能为空。");
            }
            if (!password.equals(confirm)) {
                throw new Exception("两次密码输入不一致.");
            }
            if (idParam == null || idParam.length() == 0) {
                TeacherEntity teacher = new TeacherEntity();
                teacher.setTeachername(teachername);
                teacher.setPassword(password);
                teacher.setMarketingid(marketingid);
                teacher.setStatus(1);
                teacher.setCreatetime(new java.util.Date(System.currentTimeMillis()));
                teacher.setUpdatetime(new java.util.Date(System.currentTimeMillis()));
                teacherService.create(teacher);
                
                request.getSession(true).setAttribute("teacherInSession", teacher);
                this.forward(request, response, "/teacher?command=registerDone");
            } else {
                TeacherEntity teacher = teacherService.find(Integer.valueOf(idParam));
                this.parseRequest(request, teacher);
                teacher.setUpdatetime(new java.util.Date(System.currentTimeMillis()));
                teacherService.edit(teacher);
                request.getSession(true).setAttribute("userInSession", teacher);
                this.forward(request, response, "/teacher?command=editDone");
            }
        } catch (Exception e) {
            error(request, response, e.getMessage());
        }
    }
    
    @EJB
    TeacherService teacherService;

    void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String teachername = request.getParameter("teachername");
        String password = request.getParameter("password");
        System.out.println("TeacherServlet.login() teachername = " + teachername + " password=" + password);
        TeacherEntity teacher = teacherService.findByTeachername(teachername);
        if (teacher.getPassword().equals(password)) {
            request.getSession(true).setAttribute("teacherInSession", teacher);
            System.out.println("TeacherServlet.login() teacher = " + teacher);
            List<UserEntity> studentList = teacherService.findStudentsByTeacherid(teacher.getTeacherid());
            forward(request, response, "/teacher/studentsList.jsp");
        } else {
            request.setAttribute("errMsg", "输入信息有误,无法登录.");
            forward(request, response, "/user/login.jsp");
        }
    }
}
